/*	Copyright 2013 RangerUFO
 *
 *	This file is part of Libevent-helper.
 *
 *	Libevent-helper is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Libevent-helper is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public License
 *	along with Libevent-helper.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __libevent_helper_session_h__
#define __libevent_helper_session_h__

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus

#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <sys/time.h>
#include <stddef.h>

struct evsession;

typedef void (*evsession_data_cb)(struct evsession*, void*);
typedef void (*evsession_event_cb)(struct evsession*, short, void*);
typedef enum bufferevent_filter_result (*evsession_filter_cb)(struct evbuffer*, struct evbuffer*, void*);

struct evsession* evsession_new(struct event_base* base, evutil_socket_t fd);
void evsession_free(struct evsession* s);
struct event_base* evsession_get_base(struct evsession* s);

void evsession_setcb(
		struct evsession* s,
		evsession_data_cb read_cb,
		evsession_data_cb write_cb,
		evsession_event_cb event_cb,
		void* args);
int evsession_add_filter(
		struct evsession* s,
		evsession_filter_cb input_cb,
		evsession_filter_cb output_cb,
		void* args);
void evsession_clear_filters(struct evsession* s);

int evsession_connect(struct evsession* s, const char* ip, int port);
int evsession_connect_hostname(struct evsession* s, const char* host, int port);
int evsession_get_dns_error(struct evsession* s);

int evsession_write(struct evsession* s, const void* data, size_t len);
int evsession_write_buffer(struct evsession* s, struct evbuffer* buf);
size_t evsession_read(struct evsession* s, void* data, size_t len);
int evsession_read_buffer(struct evsession* s, struct evbuffer* buf);

struct evbuffer* evsession_get_input(struct evsession* s);
struct evbuffer* evsession_get_output(struct evsession* s);

int evsession_enable(struct evsession* s, short ev);
int evsession_disable(struct evsession* s, short ev);
short evsession_get_enabled(struct evsession* s);
int evsession_set_timeouts(struct evsession* s, const struct timeval* tr, const struct timeval* tw);

int evsession_set_rate_limit(struct evsession* s, struct ev_token_bucket_cfg* cfg);
int evsession_add_to_rate_limit_group(struct evsession* s, struct bufferevent_rate_limit_group* g);
int evsession_remove_from_rate_limit_group(struct evsession* s);

size_t evsession_get_read_limit(struct evsession* s);
size_t evsession_get_write_limit(struct evsession* s);

const char* evsession_get_peer_ip(struct evsession* s);
int evsession_get_peer_port(struct evsession* s);

#ifdef __cplusplus
}
#endif  // __cplusplus

#endif  // __libevent_helper_session_h__
